#include <stdio.h>
#include <stdlib.h>
#include "./SeqStack.h"

// 初始化
// 初始化栈底指针和栈顶指针, 并且初始化最大容量
Status InitStack(Stack *stack) {
    ElemType *tmp = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
    if(tmp == NULL) {
        return ERROR;
    }
    stack->top = stack->base = tmp;
    stack->stackSize = MAXSIZE;
    return OK;
}

// 销毁
Status DestroyStack(Stack *stack) {
    free(stack->base);
    stack->top = stack->base = NULL;
    return OK;
}

// 判断是否为空
int StackEmpty(Stack stack) {
    return stack.top == stack.base;
}

// 求栈的长度
int StackLength(Stack stack) {
    return stack.top - stack.base;
}

// 取栈顶元素
Status GetTop(Stack stack, ElemType *e) {
    if(stack.top == stack.base) {
        return ERROR;
    }
    *e = *(stack.top - 1);
    return OK;
}

// 置空栈
Status ClearStack(Stack *stack) {
    stack->top = stack->base = NULL;
    return OK;
}

// 压栈
Status Push(Stack *stack, ElemType e) {
    if(stack->top - stack->base >= stack->stackSize) {
        // 满了, 上溢
        return ERROR;
    }
    *(stack->top) = e;
    stack->top++;
    return OK;
}

// 弹栈
Status Pop(Stack *stack, ElemType *e) {
    if(stack->top <= stack->base) {
        // 空表, 下溢
        return ERROR;
    }
    stack->top--;
    *e = *(stack->top);
    return OK;
}